using System;
using System.Collections.Generic;
using System.Text;

namespace Elte.SqlSpIdx.Indices
{

    public struct CacheDescriptor
    {
        private long memorysize;

        public long MemorySize
        {
            get { return memorysize; }
            set { memorysize = value; }
        }


    }

    public class Cache<Key, Type>
    {
        private int useid;
        private Dictionary<Key, Type> IndexCache = new Dictionary<Key, Type>();
        private Dictionary<Key, CacheDescriptor> DescriptionList;
        private long memorylimit;

        public long MemoryLimit
        {
            get { return memorylimit; }
            set
            {
                memorylimit = value;
                while (value < UsedBytes)
                {
                    if (!RemoveOne()) break;
                }
            }
        }
        public bool RemoveOne()
        {
            if (IndexCache.Count == 0) return false;
            return true;
        }
        public long UsedBytes
        {
            get
            {
                long bytes = 0;
                foreach (CacheDescriptor C in DescriptionList.Values)
                {
                    bytes += C.MemorySize;
                }
                return bytes;
            }
        }
        public bool Add(Key key, Type element, long memorysize)
        {
            IndexCache.Add(key, element);
            return true;
        }
        public bool Add(Key key, Type element)
        {
            return Add(key, element, 0);
        }
        public bool Contains(Key key)
        {
            return IndexCache.ContainsKey(key);
        }

        public Type Get(Key key)
        {
            if (Contains(key)) return IndexCache[key];
            return default(Type);
        }
    }
}
